package dji.ux.ext.setting.aircraft;

import androidx.annotation.NonNull;

import dji.common.flightcontroller.ConnectionFailSafeBehavior;
import dji.keysdk.DJIKey;
import dji.keysdk.FlightControllerKey;
import dji.thirdparty.io.reactivex.Completable;
import dji.thirdparty.io.reactivex.Flowable;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProviderInterface;
import dji.ux.beta.base.WidgetModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;
import dji.ux.beta.util.DataProcessor;

public class OutOfControlModel extends WidgetModel {
    private SchedulerProviderInterface schedulerProvider;
    private final DataProcessor<ConnectionFailSafeBehavior> failSafeProcessor;

    public OutOfControlModel(@NonNull DJISDKModel djiSdkModel,
                               @NonNull ObservableInMemoryKeyedStore keyedStore,
                               @NonNull SchedulerProviderInterface scheduletProviderInterface) {
        super(djiSdkModel, keyedStore);
        this.schedulerProvider = scheduletProviderInterface;
        failSafeProcessor = DataProcessor.create(ConnectionFailSafeBehavior.UNKNOWN);
    }

    public Flowable<ConnectionFailSafeBehavior> getConnectionFailSafeBehavior() {
        return failSafeProcessor.toFlowable();
    }

    public Completable setConnectionFailSafeBehavior(ConnectionFailSafeBehavior connectionFailSafeBehavior) {
        DJIKey connectionFailSafeBehaviorKey = FlightControllerKey.create(FlightControllerKey.CONNECTION_FAIL_SAFE_BEHAVIOR);
        return djiSdkModel.setValue(connectionFailSafeBehaviorKey, connectionFailSafeBehavior).subscribeOn(schedulerProvider.io());
    }

    @Override
    protected void inSetup() {
        FlightControllerKey failSafeKey =
                FlightControllerKey.create(FlightControllerKey.CONNECTION_FAIL_SAFE_BEHAVIOR);
        bindDataProcessor(failSafeKey, failSafeProcessor);
    }

    @Override
    protected void inCleanup() {

    }

    @Override
    protected void updateStates() {

    }
}
